package com.sbt.demo.config;


import java.util.HashMap;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

@Configuration
public class DataSourceConfig {
	
	public static String DEFAULT_DATASOURCE = "defaultDataSource";

	public static DataSource getDataSource(DataSource defaultDataSource){
		DynamicDataSource dataSource = new DynamicDataSource();
		Map<Object, Object> targetDataSources = new HashMap<Object, Object>();
		targetDataSources.put("defaultDataSource", defaultDataSource);
		dataSource.setTargetDataSources(targetDataSources);
	    return dataSource;
	}

	/**
	 *  Function:default数据源配置
	 */
	@Primary
	@Qualifier("defaultDataSource")
	@Bean(name={"defaultDataSource"})
	@ConfigurationProperties(value="spring.datasource")
	public DataSource defaultDataSource() {
	    return DataSourceBuilder.create().build();
	}
	
//	@Bean
//	@Primary
//	public DataSource dataSource(DataSource defaultDataSource) {
//		return DataSourceConfig.getDataSource(defaultDataSource);
//	}
}
